package com.zhss.eshop.order.price.coupon;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.promotion.domain.dto.PromotionCouponDTO;
import org.springframework.stereotype.Component;

/**
 * 满减券计算组件
 * @author suyuan
 *
 */
@Component
public class ReachDiscountCouponCalculator implements CouponCalculator {

//	@Autowired
//	private JsonExtractor jsonExtractor;

	@Override
	public Double calculate(OrderInfoDTO order, PromotionCouponDTO coupon) throws Exception {
		Double payableAmount = order.getPayableAmount();

		JSONArray rules = JSONArray.parseArray(coupon.getRule());

		for(int i = 0; i < rules.size(); i++) {
			JSONObject rule = rules.getJSONObject(i);

			Double thresholdAmount = rule.getDouble( "thresholdAmount");
			Double reduceAmount = rule.getDouble( "reduceAmount");

			if(payableAmount > thresholdAmount) {
				return reduceAmount;
			}
		}

		return 0.0;
	}

}
